/******************************************************
*  Copyright(c) pages.ilovezxl.top
*  FileName :ostime.c
*  Author : rivulet.dave
*  Description : 系统时间
*  Data : 2019-04-23
******************************************************/

/*********************************************************************
 * INCLUDES
 */

#include "stdrope.h"

#include "sallib.h"
#include "salagent.h"

#if (SAL_TIME == _ENABLE)

#include "log.h"
#include "ostime.h"

#if (SAL_PRODUCER == _SAL_LINUX_)
#include "ostime_linux.h"
#endif

/*********************************************************************
 *  EXTERNAL VARIABLES
 */

/*********************************************************************
 * MACROS
 */

#define OST_LOG(level, ...) LOG_Output(s_stOSTimeInfo.pName, LOG_LEVEL_RAW, 0, __FILENAME__, __LINE__, ## __VA_ARGS__)

/*********************************************************************
 * TYPES
 */


/*********************************************************************
 * FUNCTIONS DECLARE
 */


/*********************************************************************
 *  VARIABLES
 */

static MOD_INFO_S    s_stOSTimeInfo = { .uiModID = OS_TIME, .pName = TOSTR(OS_TIME)};
static OSTIME_BASE_S *s_pstOSTimeObj = NULL;

/*********************************************************************
 * FUNCTIONS
 */

/**
 *   @Description: 系统时间模块安装
 *   @param : NA
 *   @return: _OK/_ERR
 */
int32_t osTimeInstall(void)
{
#if (SAL_PRODUCER == _SAL_LINUX_)
    s_pstOSTimeObj = (OSTIME_BASE_S *)osTimeLinuxObjNew();
#else
#endif
    return _OK;
}

/**
 *   @Description: 系统时间模块卸载
 *   @param : NA
 *   @return: _OK/_ERR
 */
int32_t osTimeUninstall(void)
{
#if (SAL_PRODUCER == _SAL_LINUX_)
    s_pstOSTimeObj = NULL;
#else
#endif
    return _OK;
}

/**
 *   @Description: 获取系统启动运行时间戳，单位毫秒
 *   @param : NA
 *   @return: 从系统启动到现在的毫秒数
 */
ostime_t osSysTimeGet(void)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSSysTimeGet)
    {
        OST_LOG(LOG_LEVEL_ERROR, "ostime object is NULL\n");
        return 0;
    }
    return s_pstOSTimeObj->pfOSSysTimeGet();
}

/**
 *   @Description: 获取时间戳，单位微秒
 *   @param : NA
 *   @return: 格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总微秒数
 */
ostime_t osTickGet(void)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSTickGet)
    {
        OST_LOG(LOG_LEVEL_ERROR, "ostime object is NULL\n");
        return 0;
    }
    return s_pstOSTimeObj->pfOSTickGet();
}

/**
 *   @Description: 获取时间戳，单位毫秒
 *   @param : NA
 *   @return: 格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总毫秒数
 */
ostime_t osTimeGet(void)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSTimeGet)
    {
        OST_LOG(LOG_LEVEL_ERROR, "ostime object is NULL\n");
        return 0;
    }
    return s_pstOSTimeObj->pfOSTimeGet();
}

/**
 *   @Description: 设置时间戳
 *   @param : ulTimeStamp - 入参，时间戳，毫秒
 *   @return: 错误信息
 */
int32_t osTimeSet(ostime_t ulTimeStamp)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSTimeSet)
    {
        OST_LOG(LOG_LEVEL_ERROR, "ostime object is NULL\n");
        return _ERR;
    }
    return s_pstOSTimeObj->pfOSTimeSet(ulTimeStamp);
}     
      
/**  }
 *   @Description: 获取日期
 *   @param : pstDate - 出参，日期
 *   @return: NA
 */
void osDateGet(OS_DATE_S *pstDate)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSDateGet)
    {
        OST_LOG(LOG_LEVEL_ERROR, "ostime object is NULL\n");
        return;
    }
    s_pstOSTimeObj->pfOSDateGet(pstDate);
}

/**
 *   @Description: 日期转换为时间戳
 *   @param : pstDate - 入参，日期
 *   @return: 时间戳
 */
ostime_t osDate2TimeStamp(OS_DATE_S *pstDate)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSDate2TimeStamp)
    {
        OST_LOG(LOG_LEVEL_ERROR, "ostime object is NULL\n");
        return 0;
    }
    return s_pstOSTimeObj->pfOSDate2TimeStamp(pstDate);
}

/**
 *   @Description: 时间戳转换为日期
 *   @param : uiTimeStamp - 入参，时间戳
 *   @param : pstDate - 出参，日期
 *   @return: NA
 */
void osTimeStamp2Date(ostime_t ulTimeStamp, OS_DATE_S *pstDate)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSTimeStamp2Date)
    {
        OST_LOG(LOG_LEVEL_ERROR, "ostime object is NULL\n");
        return;
    }
    s_pstOSTimeObj->pfOSTimeStamp2Date(ulTimeStamp, pstDate);
}

/**
 *   @Description: 休眠，单位秒
 *   @param : uiTimeSec - 入参，休眠时间
 *   @return: NA
 */
void osSleep(uint32_t uiTimeSec)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSSleep)
    {
        OST_LOG(LOG_LEVEL_ERROR, "ostime object is NULL\n");
        return;
    }
    s_pstOSTimeObj->pfOSSleep(uiTimeSec);
}

/**
 *   @Description: 休眠，单位微秒
 *   @param : uiTimeUs - 入参，休眠时间
 *   @return: NA
 */
void osSleepus(uint32_t uiTimeUs)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSSleepus)
    {
        OST_LOG(LOG_LEVEL_ERROR, "ostime object is NULL\n");
        return;
    }
    s_pstOSTimeObj->pfOSSleepus(uiTimeUs);
}

//  ==== Generic Wait Functions ====

/// Wait for Timeout (Time Delay).
/// \param[in]     ticks         \ref CMSIS_RTOS_TimeOutValue "time ticks" value
/// \return status code that indicates the execution status of the function.
osStatus_t osDelay (uint32_t ticks)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSDelay)
    {
        return osError;
    }

    return s_pstOSTimeObj->pfOSDelay(ticks);
}

/// Wait until specified time.
/// \param[in]     ticks         absolute time in ticks
/// \return status code that indicates the execution status of the function.
osStatus_t osDelayUntil (uint64_t ticks)
{
    if (!s_pstOSTimeObj || !s_pstOSTimeObj->pfOSDelayUntil)
    {
        return osError;
    }

    return s_pstOSTimeObj->pfOSDelayUntil(ticks);
}

#endif  // SAL_TIME == _ENABLE

/************************************** The End Of File **************************************/



